package com.zhiyou.controller;

import com.zhiyou.entity.Column;
import com.zhiyou.entity.PowerCenter;
import com.zhiyou.util.ColumnUtil;
import com.zhiyou.util.DBUtil;
import com.zhiyou.util.PWUtil;
import com.zhiyou.util.UserUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

/**
 * @author sanshi
 * @desc
 * @create 2021-09-01 15:25
 */
@WebServlet("/DoLoginServlet")
public class DoLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String yan=(String)req.getSession().getAttribute("vcode");
        String ma = req.getParameter("ma");
        System.out.println(yan);
        Object[] params  = {username,password};
        String sql = "select * from sys_user where acct = ? and pwd = ?";
        DBUtil util = new DBUtil();
        boolean login = util.login(sql, params);
        HttpSession session = req.getSession();
        if (ma.equals(yan)){
            if (login){
                session.setAttribute("username",username);
//            ArrayList<User> list = util.getAllUser();
                //拿到用户集合
                UserUtil userUtil = new UserUtil();
                List<PowerCenter> list = userUtil.getPageUsers(1);
                session.setAttribute("list",list);
                //拿到column集合
                ColumnUtil columnUtil = new ColumnUtil();
                List<Column> columnList = columnUtil.getAllColumn();
                session.setAttribute("columnList",columnList);

                //拿到PowerCenter集合
                PWUtil pwUtil = new PWUtil();
                List<PowerCenter> pcList = pwUtil.getAllPC3();
                session.setAttribute("pcList",pcList);

                resp.sendRedirect("index.jsp");
            }else{
                session.setAttribute("msg","invalid username or password");
                Cookie cookie = new Cookie("name", username);
                resp.addCookie(cookie);
                resp.sendRedirect("login.jsp");
            }
        }else{
            session.setAttribute("msg","验证码错误");
            Cookie cookie = new Cookie("name", username);
            resp.addCookie(cookie);
            resp.sendRedirect("login.jsp");
        }

    }
}
